package ae.pcfc.etks.online.web.controller.file;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.activation.MimetypesFileTypeMap;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ae.pcfc.etks.online.common.delegate.ProcessStepAttachmentDelegate;
import ae.pcfc.etks.online.common.vo.UploadDocumentVO;
import ae.pcfc.etks.online.web.ConstantsAdmin;
import ae.pcfc.etks.online.web.ViewPath;
import ae.pcfc.etks.online.web.controller.BaseController;

/**
 * 
 * 
 * @author Raif.Mahmoud
 */
@Controller
@RequestMapping(ViewPath.FILE)
public class FileController extends BaseController{

	private Map<String, String> propertyConstants;

	@Resource(name = "propertyConstants")
	public void setPropertyConstants(Map<String, String> propertyConstants) {
		this.propertyConstants = propertyConstants;
	}
	
	private ProcessStepAttachmentDelegate processStepAttachmentDelegate;
	
	@Resource(name = "processStepAttachmentDelegate")
	public void setProcessStepAttachmentDelegate(ProcessStepAttachmentDelegate processStepAttachmentDelegate) {
		this.processStepAttachmentDelegate = processStepAttachmentDelegate;
	}

	/**
	 * Custom handler for download request, i.e.: GET requests for
	 * /file/download.htm
	 * 
	 * @throws IOException
	 * 
	 */
	@RequestMapping(value = ViewPath.FILE_DOWNLOAD, method = RequestMethod.GET)
	@ResponseBody
	public byte[] download(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "fileId", required = true) String fileId)
			throws IOException {

		// Retrieve file information from DB based on fileId
		// for now we will use test
		//Resetting the response to remove the Pragma cache issues		
		response.reset();
		
		UploadDocumentVO documentVO = this.processStepAttachmentDelegate.findProcessStepAttachmentByPrimaryKey(new Long(fileId));
		String fullPath = documentVO.getURL();

		org.springframework.core.io.Resource resource = new FileSystemResource(
				fullPath);
		response.setContentType(new MimetypesFileTypeMap()
				.getContentType(resource.getFile()));
		response.setContentLength((int) resource.getFile().length());
		response.setHeader("Content-Disposition", "attachment; filename=\""
				+ resource.getFile().getName() + "\"");

		return FileCopyUtils.copyToByteArray(resource.getFile());

	}

	/**
	 * Custom handler for download request, i.e.: GET requests for
	 * /file/tempview.htm
	 * 
	 * @throws IOException
	 * 
	 */
	@RequestMapping(value = ViewPath.FILE_TEMPVIEW, method = RequestMethod.GET)
	@ResponseBody
	public byte[] tempFileDownload(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "tempFileName", required = true) String tempFileName)
			throws IOException {

		// Retrieve file information from DB based on fileId
		// for now we will use test

		//Resetting the response to remove the Pragma cache issues		
		response.reset();
		
		String tempFolderPath = propertyConstants
				.get(ConstantsAdmin.TEMP_UPLOAD_FILES_PATH_KEY);
		org.springframework.core.io.Resource resource = new FileSystemResource(
				tempFolderPath + File.separator + tempFileName);

		response.setContentType(new MimetypesFileTypeMap().getContentType(resource.getFile()));
		
		
		response.setContentLength((int) resource.getFile().length());
		response.setHeader("Content-Disposition", "attachment; filename=\""
				+ resource.getFile().getName() + "\"");
		
		return FileCopyUtils.copyToByteArray(resource.getFile());

	}
/*	@RequestMapping(value = ViewPath.FILE_DELETE, method = RequestMethod.POST)
	@ResponseBody
	public Boolean deleteTempFile(
			@RequestParam(value = "tempFileName", required = true) String tempFileName)
			throws IOException {
		String tempFolderPath = propertyConstants.get(ConstantsAdmin.TEMP_UPLOAD_FILES_PATH_KEY);
		
		File uploadedFile = new File(tempFolderPath + File.separator + tempFileName);
		FileUtils.forceDelete(uploadedFile);
		return true;
	}
*/
}
